package po

import (
	"GameGo/config"
	"golang.org/x/image/math/f64"
	"math"
	"math/rand"
)

type EliteEnemy struct {
	OrdinaryEnemy
	TargetPosition f64.Vec2
	EnemySkill     int
}

func NewEliteEnemy(level int) *EliteEnemy {
	var lanes = []float64{0, config.ScreenXSize}
	x := lanes[rand.Intn(len(lanes))]
	tx := rand.Intn(config.ScreenXSize-60-60+1) + 60
	enemy := &EliteEnemy{
		OrdinaryEnemy: OrdinaryEnemy{
			EnemyLevel: level,
			X:          x - 65,
			Y:          -60,
			FireCD:     10,
			MoveType:   Elite,
			EnemyDir:   DirFromAngle(270),
			IsShotgun:  false,
		},
		TargetPosition: f64.Vec2{float64(tx), config.ScreenYSize / 3},
		EnemySkill:     50,
	}
	enemy.EnemyMaxHP = enemy.EnemyLevel * config.EnemyHP
	enemy.EnemyHP = enemy.EnemyMaxHP
	return enemy

}
func (e *EliteEnemy) EnemyMove() {

	if e.EnemySkill == 0 {

		e.Y -= 6 * e.EnemyDir[1]
		e.X += 6 * e.EnemyDir[0]
		return
	}
	if IsColliding(e.X, e.Y, 65, 65, e.TargetPosition[0]+60, e.TargetPosition[1], 30, 30) {
		e.IsShotgun = true
		return
	}
	target := e.TargetPosition
	current := f64.Vec2{e.EnemyDir[0], e.EnemyDir[1]}
	toTarget := f64.Vec2{
		target[0] - e.X,
		-(target[1] - e.Y),
	}
	maxAnglePerFrame := 0.05
	e.EnemyDir = TurnToward(current, toTarget, maxAnglePerFrame)
	e.Y -= 2 * e.EnemyDir[1]
	e.X += 2 * e.EnemyDir[0]

}
func (e *EliteEnemy) EnemyShot(Bullets *[]Bullet, gx, gy float64) {

	if !e.IsShotgun {
		return
	}
	e.FireCD--
	if e.EnemySkill == 0 {
		dx := gx - e.X
		dy := gy - e.Y
		l := math.Hypot(dx, dy)
		e.EnemyDir = f64.Vec2{dx / l, -dy / l}
		e.IsShotgun = false
		return
	} else {
		e.EnemySkill--
	}
	if e.FireCD <= 0 {

		bullet := Bullet{
			BulletType:     EnemyBullet,
			BulletMoveType: Linear,
			BulletSpeed:    6.0,
			Damage:         e.EnemyLevel,
			Life:           180,
			Scale:          1,
		}
		bullet.Position = f64.Vec2{e.X, e.Y + 30}
		bullet.Dir = DirFromAngle(270)
		var bullets [7]Bullet
		for i := range bullets {
			bullets[i] = bullet
			bullets[i].Dir = DirFromAngle(float64(180 + i*30))
		}
		*Bullets = append(*Bullets, bullets[:]...)
		e.FireCD = 10
	}

}
